package com.teama4.shop.controller;

import com.teama4.shop.domain.Specification;
import com.teama4.shop.service.SpecificationService;
import com.teama4.shop.utils.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/Specification")
public class SpecificationController {
    @Autowired
    private SpecificationService specificationService;

    @GetMapping("/listCommoditySpecification")
    public String listCommoditySpecification(int id, Model model) {
        List<Specification> list = specificationService.selectByCommodity(id);
        model.addAttribute("list", list);
        model.addAttribute("commodityId", id);
        return "/merchant/commodity-specification-list";
    }

    @GetMapping("/goToAddPage")
    public String goToAddPage(int id, int commodityId, Model model) {
        Specification specification = new Specification();
        if(id != 0) {
            specification = specificationService.selectByPrimaryKey(id);
            model.addAttribute("title", "修改商品规格");
        } else {
            specification.setCommodityId(commodityId);
            model.addAttribute("title", "增加商品规格");
        }
        model.addAttribute("specification", specification);
        return "/merchant/commodity-specification-add";
    }

    @PostMapping("/addCommoditySpecification")
    public String addCommoditySpecification(Specification specification, MultipartFile file) throws IOException {
        if(!file.isEmpty()) {
            String filePath = "/myFile/img/specificationPic";
            String filename = file.getOriginalFilename();
            String sname = filename.substring(filename.lastIndexOf("."));
            String name = UUID.randomUUID() + sname;
            specification.setPicLink(name);
            specificationService.save(specification);
            FileUtil.dictionary_add(file, filePath, name);
        }
        int commodity_id = specification.getCommodityId();
        return "redirect:/Specification/listCommoditySpecification?id=" + commodity_id;
    }

    @GetMapping("/showSpecificationDetail")
    public String showSpecificationDetail(int id, Model model) {
        Specification specification = specificationService.selectByPrimaryKey(id);
        model.addAttribute("specification", specification);
        return "/merchant/specification-list";
    }

    @GetMapping("/deleteSpecification")
    public String deleteUser(@RequestParam("id") int id) {
        Specification specification = specificationService.selectByPrimaryKey(id);
        int deleted = specificationService.deleteByPrimaryKey(id);
        return "redirect:/Specification/listCommoditySpecification?id=" + specification.getCommodityId();
    }
}
